/* @(#)GSLoginAction.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-7    建立
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSOperatorProcessor;
import com.hitrust.gs.form.GSLoginForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.menu.BuildTreeMenu;
import com.hitrust.gs.util.security.Encryptor;
/**
 * @author hitrust
 * 
 */
public class GSLoginAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
		
		GSOperatorBean tGSOperatorBean = new GSOperatorBean();
		GSLoginForm tForm = (GSLoginForm) aForm;
		
		try {
			tGSOperatorBean.setGs_oper_id(tForm.getUserId());
			tGSOperatorBean.setGs_oper_pwd(Encryptor.encrypt(tForm.getUserPwd()));
//            tGSOperatorBean.setGs_oper_pwd(tForm.getUserPwd());
            tGSOperatorBean.setGs_oper_validity("0");
			GSOperatorProcessor tGSOperatorProc = new GSOperatorProcessor();
			tGSOperatorBean = tGSOperatorProc.loginSystem(tGSOperatorBean);

            ArrayList tRs = tGSOperatorProc.getOperPrivilege(tGSOperatorBean.getGs_oper_id());
			            
            if(tRs == null || tRs.size()< 1){
                GSLogWriter.error(this,GSException.E10033_MSG);
                throw new GSException(GSException.E10033,GSException.E10033_MSG);
            }
            String[] tFunCode = new String[tRs.size()];
            for(int i=0; i<tRs.size(); i++){
                tFunCode[i] = (String)tRs.get(i);                
            }
            
			BuildTreeMenu tBuildTreeMenu = BuildTreeMenu.getInstance();
			ArrayList tList = tBuildTreeMenu.filterTreeMenu(tFunCode);
			
			//把用户权限保存起来，以备权限检查
			tRs.add("0002");// 退出系统代码
			tGSOperatorBean.setOperatorRights(tRs);
            
			tForm.getSession().setAttribute(GSBaseConstants.LOGIN, tGSOperatorBean);
			tForm.getSession().setAttribute("TreeMenu", tList);
			return GSBaseConstants.FORWARD_SUCCESS;
			
		} catch (GSException e) {
			GSLogWriter.error(this, e.getMessage());
			tForm.getRequest().setAttribute("_error", e.getMessage());
			return GSBaseConstants.FORWARD_ERROR;
		} 

	}

}
